import string

from Tkinter import *
import ImageTk
import tkFileDialog
import Image

import imageProcessor
reload(imageProcessor)
import selectionFrame
from selectionFrame import SelectionFrame
import scrolledWidget
reload(scrolledWidget)
import imageCanvas
reload(imageCanvas)
import dimentionsDialog
reload(dimentionsDialog)


def rgbString(r,g,b):
    return "#%02x%02x%02x"%(r,g,b)

FILE_MENU_COLOR = rgbString(249, 231, 27)
EDIT_MENU_COLOR = rgbString(242, 176, 47)
VIEW_MENU_COLOR = rgbString(191, 186, 143)
HELP_MENU_COLOR = rgbString(183, 185, 160)
LIBRARIES_OPPTIONS_COLOR = rgbString(208, 212, 164)
LIBRARIES_GENERAL_COLOR  = rgbString(172, 182, 177)
LIBRARIES_BUTTONS_COLOR  = rgbString(227, 183, 63)
DIMENTIONS_OPPTIONS_COLOR  = rgbString(214, 191, 146)
DIMENTIONS_GENERAL_COLOR = rgbString(183, 160, 115)
DIMENTIONS_BUTTONS_COLOR  = rgbString(228, 184, 105)    
CHANGE_BUTTON_COLOR = rgbString(205, 109, 56)

DIMENTIONS_SEPARATOR = " * "
FILE_TYPE = "png"
MENU_FONT = "ariel 12 bold"

      

class UserInterface:
    
    def __init__(self):
        self.root = Tk()
        self.root.title("Snuppi")
        self.helpWindow = None   
        self.helpText = None
        self.aboutWindow = None       
        self.aboutText = None
        self.mainFrame = Frame(self.root)
        self.mainFrame.grid()
                      
        self.scrolledCanvas = scrolledWidget.scrolledCanvas(\
            self.mainFrame, width=700, height=600, background="white")
        self.scrolledCanvas.grid(row=0,column=0)
        self.imageCanvas = imageCanvas.ImageCanvas(self.scrolledCanvas)
       
        self.toolsFrame = Frame(self.mainFrame)
        self.toolsFrame.grid(row=0, column=1)
       
        self.librariesFrame=Frame(self.toolsFrame, \
                                  background=LIBRARIES_GENERAL_COLOR)
        self.librariesSelection = SelectionFrame(self.librariesFrame, \
                                                 askLibraryDialog, \
                                                 [LIBRARIES_OPPTIONS_COLOR, \
                                                 LIBRARIES_GENERAL_COLOR, \
                                                 LIBRARIES_BUTTONS_COLOR])
        self.librariesSelection.grid()
        self.librariesFrame.grid(row=0, column=1)

        self.dimentionsFrame = Frame(self.toolsFrame, \
                                   background=DIMENTIONS_GENERAL_COLOR)
        self.dimentionsSelection = selectionFrame.SelectionFrame( \
            self.dimentionsFrame, askDimentionsDialog, [DIMENTIONS_OPPTIONS_COLOR,\
                                                   DIMENTIONS_GENERAL_COLOR,\
                                                   DIMENTIONS_BUTTONS_COLOR])
        self.dimentionsSelection.grid()
        self.dimentionsFrame.grid(row=1, column=1)
        
        self.changeButton = Button(self.toolsFrame, text="Change", \
                                   background=CHANGE_BUTTON_COLOR, \
                                   height=5, command=self.change)
        self.changeButton.grid()
        self.changeButton.grid(row=2, column=1, sticky=W+E+N+S)
                              
        menubar = Menu(self.root, font=MENU_FONT)
        filemenu =Menu(menubar, tearoff=0,\
                       background=FILE_MENU_COLOR, font=MENU_FONT)        
        filemenu.add_command(label="Open", command=self.open)
        filemenu.add_command(label="Save", command=self.save)
        filemenu.add_command(label="Save as", command=self.saveAs)
        filemenu.add_command(label="Print", command=self.printImage)
        filemenu.add_command(label="Close", command=self.close)
        filemenu.add_separator()
        filemenu.add_command(label="Exit", command=self.exit)
        menubar.add_cascade(label="File", menu=filemenu)
        editMenu = Menu(menubar, tearoff=0, background=EDIT_MENU_COLOR, \
                        font=MENU_FONT)
        editMenu.add_command(label="Undo", command=self.undo)
        editMenu.add_command(label="Redo", command=self.redo)
        menubar.add_cascade(label="Edit", menu=editMenu)
        viewMenu = Menu(menubar, tearoff=0, background = VIEW_MENU_COLOR, \
                        font=MENU_FONT)
        viewMenu.add_command(label="Zoom In", command=self.zoomIn)
        viewMenu.add_command(label="Zoom Out", command=self.zoomOut)
        menubar.add_cascade(label="View", menu=viewMenu)
        helpMenu = Menu(menubar, tearoff=0, background=HELP_MENU_COLOR,\
                        font = MENU_FONT)
        helpMenu.add_command(label="About", command=self.about)
        helpMenu.add_command(label="Help", command=self.help)
        menubar.add_cascade(label="Help", menu=helpMenu, font=MENU_FONT)
        self.root.config(menu=menubar)

        self.file = None
        self.image = None
        self.imageProcessor = None
          
        self.root.mainloop()

    def change(self):
        dimentions = self.dimentionsSelection.getChoice()[0]
        columns, rows = parseDimentions(dimentions) 
        libraries =  self.librariesSelection.getChoice()
        box = map(int, self.imageCanvas.getRectangle())

                      
        #TEMPORARILY
        basicUnitAsPrecentage = 20
        maximumDifference = 50
        
       
        self.image = self.imageProcessor.run(libraries, box, columns, rows, \
                                             basicUnitAsPrecentage,\
                                             maximumDifference )
        self.presentImage()
             
                   
    def open(self):
       self.file = tkFileDialog.askopenfilename(parent=self.root)
       if(self.file!=''):
           self.image = Image.open(self.file)
           self.imageProcessor = imageProcessor.ImageProcessor(self.file)
           self.presentImage()
                          
    def save(self):
        self.imageProcessor.save()
       
    def saveAs(self):
        #TOFIX - FILE NAME should include extention 
        filename = tkFileDialog.asksaveasfilename(parent=self.root, \
                                                  filetypes=[("Portable \
                                                  Network Graphics","*.png")])+".png"
        if (filename!=''):       
           self.imageProcessor.saveAs(filename)
                
    def printImage(self):
        print "printImage"    
    def close(self):
        print "close"
    def exit(self):
        print "exit"
        
    def undo(self):
        self.image = self.imageProcessor.undo()
        self.presentImage()
        
    def redo(self):
        self.image = self.imageProcessor.redo()
        self.presentImage()

    def zoomIn(self):
        print "zoomIn"
    def zoomOut(self):
        print "zoomOut"

    def about(self):
        self.aboutWindow = Toplevel(self.root)
        self.aboutWindow.title("About")
        self.aboutWindow.resizable(0,0)
        self.aboutWindow.transient(self.root)           
        self.aboutText = scrolledWidget.scrolledText(self.aboutWindow)
        self.aboutText.insert(END, "about text")
        self.aboutText.grid()
              

    def help(self):
        self.helpWindow = Toplevel(self.root)
        self.helpWindow.title("Help")
        self.helpWindow.resizable(0, 0)
        self.helpWindow.transient(self.root)
        self.helpText = scrolledWidget.scrolledText(self.helpWindow)
        self.helpText.insert(END, "help text")
        self.helpText.grid()

    def presentImage(self):
        self.imageCanvas.setImage(self.image)
        self.scrolledCanvas.create_window(0, 0, window=self.imageCanvas)
        self.scrolledCanvas.config(scrollregion=self.scrolledCanvas.bbox("all"))

          
def askDimentionsDialog(master):
    dialog = dimentionsDialog.DimentionsDialog(master,title="Dimentions")
    if (dialog.result != None):
        dimentions= string.join(dialog.result, DIMENTIONS_SEPARATOR)
        return dimentions
    else:
        return None


def askLibraryDialog(master):
    lib = tkFileDialog.askdirectory(parent=master, title = 'Choose directory')
    return lib
  

def parseDimentions(word):
    fields = string.split(word, DIMENTIONS_SEPARATOR)
    columns =  string.atoi(fields[0])
    rows = string.atoi(fields[1])
    return rows, columns

if __name__== "__main__":
    ui = UserInterface()
    
  